public class Stack<Element> {
    Element head;
    Stack<Element> tail;
    int height;
    
    public Stack(Element head, Stack<Element> tail) {
        this.head = head;
        this.tail = tail;
        if(tail == null)
            height = 1;
        else 
            height = 1 + tail.height;
    }
    
    public static Stack append(Stack p, Stack q) {
        while(p != null) {
            q = new Stack(p.head,q);
            p = p.tail;
        }
        return q;
    }
}
